<template>
  <el-dialog
    :visible.sync="visible"
    :before-close="handleClose"
    title="提示"
    width="30%">
    <p style="line-height: 21px; color: #333;">您将批量修改申请重修的学生成绩，请先在当前页面的更多菜单内下载Excel模板，根据汇总信息填写表格。请务必在确认表格数据正确无误后上传Excel文件！</p>
    <input
      id="fileinput"
      accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
      type="file"
      @change="uploading($event)">
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="submit($event)">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { importChongxiu } from '@/api/education'

export default {
  name: 'UploadChongxiu',
  props: {
    visible: {
      type: Boolean,
      required: true
    }
  },
  methods: {
    handleClose() {
      this.$emit('close')
    },
    uploading(event) {
      this.file = event.target.files[0] // 获取文件

      var windowURL = window.URL || window.webkitURL

      this.file = event.target.files[0]

      // 创建图片文件的url
      this.src = windowURL.createObjectURL(event.target.files[0])
    },
    submit() {
      event.preventDefault() // 取消默认行为
      const formdata = new FormData()
      formdata.append('file', this.file)
      // 此处必须设置为  multipart/form-data

      importChongxiu(formdata).then(res => {
        let type = 'error'
        if (res.code === 200) {
          type = 'success'
          this.$emit('close')
        }
        this.$message({ type, message: res.msg })
      }).catch(() => {})
    }
  }
}
</script>

<style scoped>

</style>
